<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="s"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<!DOCTYPE html>
<html lang="en">
<head>
<title><s:message code="app.title" /></title>
<%@ include file="Include.jsp"%>
<script type="text/javascript">
	
$(document).ready(function() {
	var oTable = $('#branchTable').dataTable( {
		 	"bDestroy": true,
	        "bProcessing": false,
	        "iDisplayLength": 7,
	        "bFilter": false,
	        "bLengthChange": false,
	        "aoColumnDefs": [
	          { 'bSortable': false, 'aTargets': [7] }
	       	]
	 	} );
	
	$('#addBranch').click(function(){
		$('#branchForm').attr('action','AddBranch');
		$('#companyId').val($('#companyid').val());
		$('#branchForm').attr('method','get');
		$('#branchForm').submit();
	});
	
});

function editBranch(branchid){
	$('#branchForm').attr('action','EditBranch');
	
	$('#branchid').val(branchid);
	$('#companyId').val($('#companyid').val());
	$('#branchForm').attr('method','get');
	$('#branchForm').submit();
}
	
$(document).ready(function() {
	$("#companyname").focus();
	
	$('#editCompany').click(function(){
		$('#companyForm').attr('action','EditCompany');
		$('#companyId').val($('#companyid').val());
		$('#companyForm').attr('method','post');
		$('#companyForm').submit();
	});
});

</script>
</head>
<body>
	<div class="container">
		<%@ include file="Menu.jsp"%>
		<h2><s:message code="companyForm.editheader" /></h2>
		<hr>
		<form:form id="cancelForm">			
		</form:form>
		<form:form class="form-horizontal" method="post" name="companyForm" id="companyForm" commandName="companyForm"
	           modelAttribute="companyForm" >
			
			<table class="table table-bordered">
			<tr>
				<th width="15%"><form:label path="companyname"><s:message code="companyForm.companyname" />
				<font color="red">*</font>
				</form:label></th>
				<td width="*">
				<form:hidden path="companyid" cssClass="input-medium" id="companyid" />
				<form:input path="companyname" cssClass="input-medium" id="companyname" maxlength="40" size="40" tabindex="1" />
				<form:errors cssClass="text-error" path="companyname" />
				</td>

				<th width="15%"><form:label path="address"><s:message code="companyForm.address" /></form:label></th>
				<td width="*">
					<form:textarea path="address" rows="4" id="address" cssClass="input-xlarge"/>
					<form:errors cssClass="text-error" path="address" />
				</td>
			</tr>
			<tr>
				 <th width="15%"><form:label path="cityId"><s:message code="companyForm.cityName" /></form:label></th>
				 <td width="*">
					<form:select path="cityId" cssClass="input-medium" id="cityId" >
						<form:option value="">--SELECT-</form:option>
						<form:options items="${cityList}" itemLabel="cityname" itemValue="cityid"/>
					</form:select>
					<form:errors cssClass="text-error" path="cityId" />
				 </td>
			</tr>
			<tr>
				<th><form:label path="ctcperson"><s:message code="companyForm.ctcperson" />
				<font color="red">*</font>
				</form:label></th>
				<td><form:input path="ctcperson" cssClass="input-xlarge" id="ctcperson" maxlength="50" />
					<form:errors cssClass="text-error" path="ctcperson" />
				</td>
			</tr>
			<tr>
				<th><form:label path="email"><s:message code="companyForm.email" />
				<font color="red">*</font>
				</form:label></th>
				<td><form:input path="email" cssClass="input-xlarge" id="email" maxlength="50" />
					<form:errors cssClass="text-error" path="email" />
				</td>
			</tr>
			<tr>
				<th><form:label path="cellno"><s:message code="companyForm.cellno" /></form:label></th>
				<td><form:input path="cellno" cssClass="input-medium" id="cellno" maxlength="10" size="10" />
					<form:errors cssClass="text-error" path="cellno" />
				</td>
			</tr>
			<tr>
				<th><form:label path="website"><s:message code="companyForm.website" /></form:label></th>
				<td><form:input path="website" cssClass="input-xlarge" id="website" maxlength="50" />
					<form:errors cssClass="text-error" path="website" />
				</td>
			</tr>
			<tr>
				<th><form:label path="comments"><s:message code="companyForm.comments" /></form:label></th>
				<td>
					<form:textarea path="comments" rows="4" id="comments" cssClass="input-xlarge"/>
					<form:errors cssClass="text-error" path="comments" />
				</td>
			</tr>
			<tr>
				<th><form:label path="isactive"><s:message code="companyForm.isactive" /></form:label></th>
				<td colspan="2"><form:select path="isactive" cssClass="input-small" id="isactive" >
						<form:option value="0">NO</form:option>
						<form:option value="1">YES</form:option>
					</form:select>
					<form:errors cssClass="text-error" path="isactive" />
				</td>
			</tr>
		</table>
		<br>
			<button class="btn btn-primary" type="button" id="editCompany" ><s:message code="add.edit" /></button>
			<button class="btn btn-primary" type="submit" id="cancelCompany" formaction="ListCompany" formmethod="get" form="cancelForm"><s:message code="app.cancel" /></button>
		</form:form>
		<br/>
		<br/>
		<form id="branchForm" name="branchForm">
		<c:if test="${not empty successMessage}">
			<div class="alert alert-success">
				${successMessage}
			</div>
		</c:if>
		<table border="0" width="100%">
			<tr>
				<td align="left">
					<h2><s:message code="branchForm.listHeader"></s:message></h2>
				</td>
				<td align="right">
					<h5 align="right"><a href="#" id="addBranch" ><s:message code="branchForm.addheader"></s:message></a></h5>
				</td>
			</tr>
		</table>
		<br/>	
		<table class="table table-hover table-bordered" id="branchTable">
				<thead>
					<tr>
						<th><s:message code="branchForm.branchname" /></th>
						<th><s:message code="branchForm.branchcode" /></th>
						<th><s:message code="branchForm.cityName" /></th>
						<th><s:message code="branchForm.ctcperson" /></th>
						<th><s:message code="branchForm.email" /></th>
						<th><s:message code="branchForm.cellno" /></th>
						<th><s:message code="branchForm.website" /></th>
						<th><s:message code="branchForm.isactive" /></th>
						<th><s:message code="branchForm.creationdate" /></th>
						<th><s:message code="app.action" /></th>
					</tr>
				</thead>
				
				<tbody>
					<c:forEach items="${branchList}" var="branch" >
						<tr>
							<td>
								<c:out value="${branch.branchname}"></c:out>
							</td>
							<td>
								<c:out value="${branch.branchcode}"></c:out>
							</td>
							<td>
								<c:out value="${branch.city.cityname}"></c:out>
							</td>
							<td>
								<c:out value="${branch.ctcperson}"></c:out>
							</td>
							<td>
								<c:out value="${branch.email}"></c:out>
							</td>
							<td>
								<c:out value="${branch.cellno}"></c:out>
							</td>
							<td>
								<c:out value="${branch.website}"></c:out>
							</td>
							<td>
								<c:choose>
								    <c:when test="${branch.isactive == 1}">
								       YES
								    </c:when>
								    <c:otherwise>
								        NO
								    </c:otherwise>
								</c:choose>
							</td>
							<td>
								<fmt:formatDate value="${branch.creationdate}" pattern="dd/MM/yyyy"/>
							</td>
							<td>
								<a href="#" onclick="javascript: editBranch('${branch.branchid}');" >Edit</a>
							</td>
						</tr>
					</c:forEach>
				</tbody>
		</table>
		<input type="hidden" name="branchid" id="branchid"> 
		<input type="hidden" name="companyId" id="companyId"> 
		</form>
		 <%@ include file="Footer.jsp"%>
	</div>
</body>
</html>